package com.zb.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zb.entity.ZjWithdrawOrder;
import com.zb.mapper.ZjWithdrawOrderMapper;
import com.zb.service.ZjWithdrawOrderService;
import com.zb.vo.WithdrawLogVo;
import com.zb.vo.WithdrawOrderVo;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 提现工单表 服务实现类
 * </p>
 *
 * @author WEN
 * @since 2024-05-27
 */
@Service
public class ZjWithdrawOrderServiceImpl extends ServiceImpl<ZjWithdrawOrderMapper, ZjWithdrawOrder> implements ZjWithdrawOrderService {

    // 保存提现工单
    @Override
    public void save(Long custAccId, Long withdrawId, List<WithdrawOrderVo> orderVos) {
        if (CollectionUtil.isEmpty(orderVos)) {
            return;
        }
        List<ZjWithdrawOrder> adds = new ArrayList<>();
        List<ZjWithdrawOrder> updates = new ArrayList<>();
        ZjWithdrawOrder withdrawOrder = null;
        for (WithdrawOrderVo vo : orderVos) {
            withdrawOrder = new ZjWithdrawOrder();
            BeanUtil.copyProperties(vo, withdrawOrder);
            if (null != withdrawOrder.getId()) {
                updates.add(withdrawOrder);
                continue;
            }
            withdrawOrder.setId(IdWorker.getId());
            withdrawOrder.setCustAccId(custAccId);
            withdrawOrder.setWithdrawId(withdrawId);
            withdrawOrder.setOldWithdrawMoney(null == withdrawOrder.getOldWithdrawMoney()? withdrawOrder.getWithdrawMoney() : withdrawOrder.getOldWithdrawMoney());
            adds.add(withdrawOrder);
        }
        if (CollectionUtil.isNotEmpty(adds)) this.saveBatch(adds);
        if (CollectionUtil.isNotEmpty(updates)) this.updateBatchById(updates);

    }

    // 根据提现单ID获取提现工单
    @Override
    public List<ZjWithdrawOrder> getByWithdrawId(Long withdrawId) {
        QueryWrapper<ZjWithdrawOrder> wrapper = new QueryWrapper<>();
        wrapper.select("id", "work_id", "work_no", "withdraw_money", "old_withdraw_money")
                .eq("withdraw_id", withdrawId);
        return this.list(wrapper);
    }

    // 根据提现单ID获取提现工单
    @Override
    public List<ZjWithdrawOrder> getByWithdrawIds(List<Long> withdrawIds) {
        QueryWrapper<ZjWithdrawOrder> wrapper = new QueryWrapper<>();
        wrapper.select("id", "work_id", "work_no", "withdraw_money", "old_withdraw_money")
                .in("withdraw_id", withdrawIds);
        return this.list(wrapper);
    }
}
